﻿using System;
using System.ComponentModel;
using System.Linq;
using System.Web.UI;

namespace Utils.UIPermission
{
    /// <summary>
    /// 
    /// </summary>
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:CoolitePermitPanel runat=server></{0}:CoolitePermitPanel>")]
    public class CoolitePermitPanel : Coolite.Ext.Web.Panel
    {
        /// <summary>
        /// Gets or sets the name of the permission.
        /// </summary>
        /// <value>
        /// The name of the permission.
        /// </value>
        public string PermissionName
        {
            set
            {
                this.ViewState["__Name"] = value;
            }
            get
            {
                return this.ViewState["__Name"] as string;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether [hide as disabled].
        /// </summary>
        /// <value>
        ///   <c>true</c> if [hide as disabled]; otherwise, <c>false</c>.
        /// </value>
        public bool HideAsDisabled
        {
            set
            {
                this.ViewState["__HideAsDisabled"] = value;
            }
            get
            {
                object obj = this.ViewState["__HideAsDisabled"];
                return (obj == null ? false : bool.Parse(obj.ToString()));
            }
        }

        /// <summary>
        /// Raises the <see cref="E:PreRender"/> event.
        /// </summary>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected override void OnPreRender(EventArgs e)
        {
            if (!string.IsNullOrEmpty(PermissionName))
            {
                bool bCanshow = UIPermissions.GetPermissions().Contains(PermissionName);
                if (!HideAsDisabled)
                    this.Visible = bCanshow;
                else
                    this.Disabled = !bCanshow;
            }
            base.OnPreRender(e);
        }
    }


}
